<!-- subpkg_medicine/payment/index.vue -->
<script setup>
	import {ref} from 'vue';
	import {getressListAPI,preOrderAPI,createOrderAPI} from '@/apis/doct.js';
    import { paymentAPI } from '@/apis/home';

	// 弹出层组件
	const payRef = ref(null)
	// 待支付订单 ID
	const orderId = ref('')

	const props = defineProps({
		// 接收地址传的 id
		id: String
	})
	// 调用接口获取地址信息
	const resslist = ref({})
	const getressList = () => {
		getressListAPI().then((res) => {
			console.log('获取地址列表', res);
			resslist.value = res.data[0]
		})
	}


	// 获取预付药品信息
	const preOrderList = ref({})
	const getpreOrder = () => {
		// console.log(props.id);
		preOrderAPI(props.id).then((res) => {
			console.log('获取预付药品信息', res);
			preOrderList.value = res.data
		})
	}
	// 支付按钮
	const onsubmit = () => {
		console.log('支付成功');
		uni.showLoading()
		createOrderAPI({
			id: props.id,
			addressId: resslist.value.id
		}).then((res) => {
			console.log('支付', res);
			orderId.value = res.data.id
			payRef.value.open()
		}).finally(() => {
			uni.hideLoading()
		})

	}
	// 选择支付方式
	// const payChange=(e)=>{
	//  console.log(e);
	// }

	// 确认支付
	const paymentfirm = ({index}) => {
		console.log('支付方式',index);
		console.log('支付成功');
		if (index == 0) {
			return uni.showToast({
				title: '暂不支持微信支付',
				icon: 'none'
			})
		}
		paymentAPI({
			paymentMethod: index,
			orderId: orderId.value,
			payCallback: 'http://localhost:5173/#/subpack_medicine/payResult/index', //这个地址是支付完成后你返回自己小程序或者开发的app某个页面的地址
		}).then((res) => {
			console.log('支付成功',res);
			// #ifdef H5
			// 支付接口请求成功后端就返回一个跳转路径 引导你支付
			window.location.href = res.data.payUrl 
			// #endif

			// #ifdef MP-WEIXIN
			//  引导你支付
			wx.requestPayment({
				//4个参数
			})
			// #endif

		})
	}


	// 调用接口获取地址信息
	getressList()
	// 获取预付药品信息
	getpreOrder()
</script>

<template>
	<scroll-page background-color="#f6f6f6">
		<view class="medicine-page">
			<view class="page-header">
				<view class="order-shippment">
					<view class="region">
						<uni-icons size="16" color="#FF7702" type="location-filled" />
						{{resslist.city}}-{{resslist.county}}
					</view>
					<view class="detail"> {{resslist.addressDetail}} </view>
					<view class="receiver"> 李富贵 {{resslist.mobile}}</view>
				</view>
			</view>

			<view class="order-shop">
				优医药房
				<text class="alt"> 优医质保 假一赔十 </text>
			</view>
			<!-- 药品列表 -->
			<view class="medicine-list">
				<view class="medicine-list-item" v-for="(item,index) in preOrderList.medicines">
					<image class="medicine-cover" :src="item.avatar" mode="aspectFill" />
					<view class="medicine-info">
						<text class="name"> {{item.name}}</text>
						<text class="unit symbol">{{item.prescriptionFlag}} 盒 </text>
						<text class="price">¥{{item.amount}}</text>
					</view>
					<view class="quantity">x{{item.quantity}}</view>
					<view class="guide"> 用法用量：{{item.usageDosag}} </view>
				</view>
			</view>

			<!-- 订单信息 -->
			<view class="order-info">
				<uni-list :border="false">
					<uni-list-item title=" 药品金额 " :right-text="'￥'+preOrderList.couponDeduction" />
					<uni-list-item title=" 运费 " :right-text="'￥'+preOrderList.expressFee" />
					<uni-list-item title=" 优惠券 " show-arrow :right-text="'￥'+preOrderList.pointDeduction" />
					<uni-list-item title=" 实付款 " :right-text="'￥'+preOrderList.actualPayment" />
				</uni-list>
			</view>

			<!-- 底部 -->
			<view class="toolbar">
				<view class="total-amount">
					需付款: <text class="number">¥{{preOrderList.payment}}</text>
				</view>
				<view class="buttons">
					<button class="uni-button" @click="onsubmit"> 立即支付 </button>
				</view>
			</view>
		</view>
	</scroll-page>
	<paymentComp ref='payRef' :OrderID='orderId' :amount='preOrderList.payment' @payclose='payRef.close()'
		 @payconfirm='paymentfirm' />
</template>

<style lang="scss">
	// subpkg_medicine/payment/index.scss
	.medicine-page {
		padding-bottom: calc(env(safe-area-inset-bottom) + 200rpx);
	}

	.page-header {
		padding: 0 30rpx 30rpx;
		// 需要一个高为 300 的背景图
		background-color: #eaf8f6;
		overflow: hidden;

		.order-shippment {
			line-height: 1;
			padding: 30rpx;
			background-color: #fff;
			border-radius: 20rpx;
			margin-top: 30rpx;
			position: relative;

			.region {
				display: flex;
				align-items: center;
				font-size: 26rpx;
				color: #6f6f6f;
			}

			.detail {
				margin: 24rpx 0;
				font-size: 34rpx;
				color: #121826;
			}

			.receiver {
				font-size: 26rpx;
				color: #121826;
			}
		}
	}

	.order-shop {
		padding: 20rpx 30rpx;
		margin: 10rpx 0;
		line-height: 1;
		font-size: 32rpx;
		color: #121826;
		font-weight: 500;

		.alt {
			font-size: 24rpx;
			color: #979797;
			margin-left: 10rpx;
		}
	}

	.medicine-list {
		padding: 0 30rpx;
		background-color: #fff;
	}

	.medicine-list-item {
		padding: 30rpx 0;
		border-bottom: 1rpx solid #ededed;
		position: relative;

		&:last-child {
			border-bottom: none;
		}

		.medicine-cover {
			width: 180rpx;
			height: 160rpx;
			float: left;
		}

		.medicine-info {
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			height: 160rpx;
			padding-left: 20rpx;

			.name {
				font-size: 30rpx;
				font-weight: 500;
				color: #121826;
			}

			.unit {
				color: #979797;
				font-size: 28rpx;
				display: flex;
				align-items: center;

				@include text-overflow(1);

				&.symbol::before {
					content: ' 处方药 ';
					line-height: 1;
					padding: 8rpx 10rpx;
					margin-right: 10rpx;
					border-radius: 4rpx;
					font-size: 24rpx;
					color: #fff;
					background-color: #16c2a3;
				}
			}

			.price {
				color: #eb5757;
				font-size: 32rpx;
			}
		}

		.quantity {
			top: 30rpx;
			right: 0;
			position: absolute;

			font-size: 30rpx;
			color: #121826;
		}

		.guide {
			clear: both;
			padding: 20rpx;
			margin-top: 30rpx;
			font-size: 28rpx;
			color: #848484;
			border-radius: 10rpx;
			background-color: #f6f6f6;
		}
	}

	.order-info {
		padding: 0 30rpx;
		margin-top: 30rpx;
		background-color: #fff;

		.right-text {
			display: flex;
			align-items: center;
		}

		.copy {
			line-height: 1;
			padding: 6rpx 20rpx 4rpx;
			margin-right: 20rpx;
			font-size: 24rpx;
			color: #16c2a3;
			background-color: #fafafa;
			border-radius: 40rpx;
			border: 1rpx solid #16c2a3;
		}

		.number {
			font-size: 32rpx;
			color: #848484;
		}

		:deep(.uni-list-item__container) {
			padding-left: 0 !important;
			padding-right: 0 !important;
		}

		:deep(.uni-list-item__content-title) {
			font-size: 32rpx !important;
			color: #3c3e42 !important;
		}

		:deep(.uni-list-item__extra-text) {
			font-size: 32rpx !important;
			color: #848484 !important;
		}

		:deep(.uni-icon-wrapper) {
			padding: 0 !important;
			margin-right: -10rpx !important;
			font-size: 36rpx !important;
			margin-top: 2rpx;
		}
	}

	.toolbar {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;

		display: flex;
		align-items: center;
		height: 88rpx;
		padding: 30rpx 40rpx calc(env(safe-area-inset-bottom) + 30rpx);
		background-color: #fff;

		.buttons {
			flex: 1;
			display: flex;
		}

		.uni-button {
			flex: 1;
			padding: 0 30rpx;
		}

		.total-amount {
			display: flex;
			align-items: center;
			width: 270rpx;
			font-size: 28rpx;
			color: #3c3e42;
		}

		.number {
			font-size: 40rpx;
			color: #eb5757;
			margin-left: 10rpx;
		}
	}
</style>